﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Athanor.Utils
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class DebugComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game game;
        SpriteBatch spriteBatch;
        SpriteFont debugFont;

        //FPS
        int stableFPS, lastSecond, countFPS;

        //Safe rect
        Texture2D dummyTexture;
        Color translucentRed = new Color(Color.Red, 0.2f);
        Rectangle leftBorder, rightBorder, topBorder, bottomBorder;

        public DebugComponent(Game game)
            : base(game)
        {
            this.game = game;
            DrawOrder = 1000;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            countFPS = 0;
            stableFPS = 0;
            lastSecond = 0;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            debugFont = Game.Content.Load<SpriteFont>("AthanorFont");

            // Create a 1x1 white texture.
            dummyTexture = new Texture2D(GraphicsDevice, 1, 1);
            dummyTexture.SetData(new Color[] { Color.White });

            // Look up the current viewport and safe area dimensions.
            Viewport viewport = GraphicsDevice.Viewport;

            Rectangle safeArea = viewport.TitleSafeArea;

            int viewportRight = viewport.X + viewport.Width;
            int viewportBottom = viewport.Y + viewport.Height;

            // Compute four border rectangles around the edges of the safe area.
            leftBorder = new Rectangle(viewport.X, viewport.Y, safeArea.X - viewport.X, viewport.Height);
            rightBorder = new Rectangle(safeArea.Right, viewport.Y, viewportRight - safeArea.Right, viewport.Height);
            topBorder = new Rectangle(safeArea.Left, viewport.Y, safeArea.Width, safeArea.Top - viewport.Y);
            bottomBorder = new Rectangle(safeArea.Left, safeArea.Bottom, safeArea.Width, viewportBottom - safeArea.Bottom);

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            countFPS++;
            if (gameTime.TotalRealTime.Seconds > lastSecond)
            {
                lastSecond = gameTime.TotalRealTime.Seconds;
                stableFPS = countFPS;
                countFPS = 0;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.DrawString(debugFont, "FPS: " + stableFPS, new Vector2(100, 100), Color.Yellow, 0, Vector2.Zero, 2, SpriteEffects.None, 0);

            spriteBatch.Draw(dummyTexture, leftBorder, translucentRed);
            spriteBatch.Draw(dummyTexture, rightBorder, translucentRed);
            spriteBatch.Draw(dummyTexture, topBorder, translucentRed);
            spriteBatch.Draw(dummyTexture, bottomBorder, translucentRed);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}